<template>
	<div class="addressedit">
		<van-address-edit :area-list="areaList" show-postal show-delete show-set-default show-search-result :address-info="info"
		 :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" @delete="onDelete"
		 @change-detail="onChangeDetail" />
	</div>
</template>


<script>
	import {
		Toast
	} from 'vant';
	import {
		areaList
	} from "@vant/area-data";
	import {
		getAddressDetail,
		updateAddress,
		addAddress
	} from "../api/index.js";
	
	export default {
		name: 'addressedit',
		data() {
			return {
				areaList: Object.freeze(areaList),
				id: "",
				searchResult: [],
				info: {
					// id	
					// name	
					// tel	
					// province	
					// city	
					// county
					// addressDetail	
					// areaCode	
					// postalCode
					// isDefault
				}
			}
		},
		created() {
			this.id = this.$route.query.addressId;
			if (this.id) {
				//编辑
				this.getData();
			}
		},
		methods: {
			getareaCode(regionName){
				//console.log(this.areaList.county_list);
				for(let key in this.areaList.county_list){
					if(regionName==this.areaList.county_list[key]){
						return key;
					}
				}
			},
			getData() {
				getAddressDetail(this.id).then(data => {
					//console.log(data);
					if (+data.resultCode == 200) {
						let {
							addressId,
							userName,
							userPhone,
							defaultFlag,
							provinceName,
							detailAddress,
							cityName,
							regionName
						} = data.data;
						
						this.info = {
							id: addressId,
							name: userName,
							tel: userPhone,
							isDefault: defaultFlag,
							province: provinceName,
							addressDetail: detailAddress,
							city: cityName,
							county: regionName,
							postalCode: "100000",
							areaCode: this.getareaCode(regionName)
						}
						
					}
				})
			},
			onSave(context) {
				//console.log(context);
				let {addressDetail,
						areaCode,
						city,
						country,
						county,
						id,
						isDefault,
						name,
						postalCode,
						province,
						tel}=context;
						
				let obj={
					userName:name,
					userPhone:tel,
					defaultFlag:isDefault?1:0,
					provinceName:province,
					detailAddress:addressDetail,
					cityName:city,
					regionName:county,
					areaCode,
					postalCode,
				}
				if(this.id){//编辑
					updateAddress({addressId:id,...obj}).then(data=>{
						if(data.resultCode){
							this.$router.push("/addressList");
						}
					})
				}else{//新增
					addAddress(obj).then(data=>{
						if(data.resultCode){
							this.$router.push("/addressList");
						}
					})
				}
			
			},
			onDelete() {
				Toast('delete');
			},
			onChangeDetail(val) {
				if (val) {
					this.searchResult = [{
						name: '黄龙万科中心',
						address: '杭州市西湖区',
					}, ];
				} else {
					this.searchResult = [];
				}
			},
		},
	}
</script>

<style>
</style>
